package com.dzx;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年12月07日 12:58:16
 */
public class FileSystemUtil {



    public   static  Configuration  configuration ;

    public  static   FileSystem fileSystem;

    public static final String HDFS_PATH = "hdfs://192.168.42.85:8020";

    static {
        try {
            configuration = new Configuration();
            fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将本地的文件复制到hdfs上面
     * @param localFile
     * @param hdfsFile
     */
    public  static  void  copyFromLocal(String localFile,String hdfsFile) throws IOException {
         fileSystem.copyFromLocalFile(new Path(localFile),new Path(hdfsFile));
    }

    /**
     * 将hdfs上的文件拷贝到本地
     * @param hdfsFile
     * @param localFile
     * @throws IOException
     */
    public  static  void  copyToLocal(String hdfsFile,String localFile) throws IOException {
         fileSystem.copyToLocalFile(new Path(hdfsFile),new Path(localFile));
    }


    public  static  void  delete(String path) throws IOException {
         fileSystem.delete(new Path(path),true);
    }

}
